package com.lwj.service.impl;

import com.lwj.dao.ProgressMapper;
import com.lwj.entity.Progress;
import com.lwj.model.PlaneList;
import com.lwj.service.ProgressService;
import com.lwj.util.DataRet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author linwj
 * @date 2018/5/12 14:59
 */
@Transactional
@Service
public class ProgressImpl implements ProgressService {

    @Autowired
    private ProgressMapper progressMapper;

    @Override
    public DataRet<String> makePlane(PlaneList planeList, int projectId) {
        insertPlane(planeList.getStartDate1(),planeList.getEndDate1(),projectId,planeList.getRemark1());
        insertPlane(planeList.getStartDate2(),planeList.getEndDate2(),projectId,planeList.getRemark2());
        insertPlane(planeList.getStartDate3(),planeList.getEndDate3(),projectId,planeList.getRemark3());
        return new DataRet<>("操作成功");
    }

    @Override
    public DataRet<List<Progress>> getPlaneProgress(int projectId) {
        return new DataRet<>(progressMapper.selectByProjectId(projectId));
    }

    @Override
    public DataRet<String> changeProgressStatus(int progressId) {
        Progress progress=new Progress();
        progress=progressMapper.selectByPrimaryKey(progressId);
        if ("0".equals(progress.getType())){
            progress.setType("1");
        }else if ("1".equals(progress.getType())){
            progress.setType("0");
        }
        progressMapper.updateByPrimaryKey(progress);
        return new DataRet<>("修改成功");
    }

    private void insertPlane(Date startDate,Date endDate,int projectId,String remark){
        Progress progress=new Progress();
        progress.setCreateTime(new Date());
        progress.setStartTime(startDate);
        progress.setEndTime(endDate);
        progress.setRemark(remark);
        progress.setType("0");
        progress.setProjectId(projectId);
        progressMapper.insert(progress);
    }
}
